1 module hip.data.jsonc;
2 public import hip.data.json;
3 auto parseJSONC(string jsonc)
4 {
5     return parseJSON(stripComments(jsonc));
6 }
7 
8 /** 
9 *   Strips single and multi line comments (C style)
10 */
11 string stripComments(string str)
12 {
13     string ret;
14     size_t i = 0;
15     size_t length = str.length;
16     ret.reserve(str.length);
17 
18     while(i < length)
19     {
20         //Don't parse comments inside strings
21         if(str[i] == '"')
22         {
23             size_t left = i;
24             i++;
25             while(i < length && str[i] != '"')
26             {
27                 if(str[i] == '\\')
28                     i++;
29                 i++;
30             }
31             i++; //Skip '"'
32             ret~= str[left..i];
33         }
34         //Parse single liner comments
35         else if(str[i] == '/' && i+1 < length && str[i+1] == '/')
36         {
37             i+=2;
38             while(i < length && str[i] != '\n')
39                 i++;
40         }
41         //Parse multi line comments
42         else if(str[i] == '/' && i+1 < length && str[i+1] == '*')
43         {
44             i+= 2;
45             while(i < length)
46             {
47                 if(str[i] == '*' && i+1 < length && str[i+1] == '/')
48                     break;
49                 i++;
50             }
51             i+= 2;
52         }
53         //Safe check to see if it is in range
54         if(i < length)
55             ret~= str[i];
56         i++;
57     }
58     return ret;
59 }